﻿Imports System.Net.Mail
Partial Class WebForm_SelfManageWorkHeader_SelfAddWorkHeader
    Inherits System.Web.UI.UserControl
    Public rboOther As String = "其他" '髒亂地區類別(其他)
    Public gvDirtyItem As String = "其他" '髒亂種類(其他)

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub rdoLocationType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rdo As RadioButtonList = sender
        TryCast(Me.fv.FindControl("mvReportDirtyWilling"), MultiView).ActiveViewIndex = rdo.SelectedIndex
    End Sub

    Protected Sub ddlTownID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BindRoadID()
        Me.BindRoadItem()
    End Sub

    Protected Sub ddlRoadID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BindRoadItem()
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Me.BindTownID()
        Me.BindRegionType()
        Me.BindDirtyItemForInsert()

        If Me.fv.CurrentMode = FormViewMode.Insert Then
            '新增
            Dim MyRandom As New clsRandom
            TryCast(fv.FindControl("txtRandomCasePassword"), TextBox).Text = MyRandom.Generate
            TryCast(fv.FindControl("lblRandomCasePasswordValue"), Label).Text = TryCast(fv.FindControl("txtRandomCasePassword"), TextBox).Text
            TryCast(Me.fv.FindControl("lbtnUploadB"), LinkButton).Visible = False
            TryCast(Me.fv.FindControl("txtReportDate"), TextBox).Text = DateTime.Today().ToString("yyyy/MM/dd")
            MyObj.SetDateLocale("zh-TW")
            TryCast(fv.FindControl("lblReportDateValue"), Label).Text = DateTime.Today().ToString("yyyy.MM.dd")
            MyObj.SetDateLocale("")
            If TryCast(Me.fv.FindControl("txtCasePassword"), TextBox).Text = "" Then
                TryCast(Me.fv.FindControl("txtCasePassword"), TextBox).Text = MyObj.GetCasePassword()
                TryCast(Me.fv.FindControl("lblCasePasswordValue"), Label).Text = TryCast(Me.fv.FindControl("txtCasePassword"), TextBox).Text
            End If
            TryCast(Me.fv.FindControl("lbtnUploadA"), LinkButton).Attributes("onclick") = Me.UploadString(TryCast(Me.fv.FindControl("txtCasePassword"), TextBox).Text, "PictureA")
            TryCast(Me.fv.FindControl("lbtnUploadB"), LinkButton).Attributes("onclick") = Me.UploadString(TryCast(Me.fv.FindControl("txtCasePassword"), TextBox).Text, "PictureB")

            '有登入則顯示案件編號;沒登入則顯示案件代碼
            If Me.Page.User.Identity.Name <> "" Then
                TryCast(Me.fv.FindControl("trRandomCasePassword"), HtmlTableRow).Visible = False
            Else
                TryCast(Me.fv.FindControl("trCasePassword"), HtmlTableRow).Visible = False
            End If
        End If

    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            '其他
            If TryCast(e.Row.FindControl("chkDirtyItemName"), CheckBox).Text.Trim() = Me.gvDirtyItem Then
                TryCast(e.Row.FindControl("txtDirtyItemName"), TextBox).Visible = True
                'e.Row.Visible = False
            Else
                TryCast(e.Row.FindControl("txtDirtyItemName"), TextBox).Visible = False
                'e.Row.Visible = True
            End If

            'Me.BindDirtyLevel(TryCast(e.Row.FindControl("ddlDirtyLevel"), DropDownList))
        End If
    End Sub

    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim dt As New Data.DataTable
        If e.CommandName = "btnSave" Then
            '驗證碼
            If TryCast(Me.fv.FindControl("txtVerificationCode"), TextBox).Text <> TryCast(Me.fv.FindControl("VerificationCode1"), Public_VerificationCode).VerificationCode Then
                MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_InvalidVerificationCode"))
                Return
            End If

            '髒亂地區類別,[其他]而未填入值 
            If TryCast(Me.fv.FindControl("rboRegionType"), RadioButtonList).SelectedValue = Me.rboOther And TryCast(Me.fv.FindControl("txtOtherRegionType"), TextBox).Text.Trim() = "" Then
                MyObj.ShowMessage(Me.GetLocalResourceObject("MustOtherRegionType"))
                Return
            Else

                '髒亂種類/程度選擇,沒有做任何選擇
                Me.dirtyItemField = SetDirtyItem()

                If Not HaveItem Then
                    MyObj.ShowMessage(Me.GetLocalResourceObject("MustDirtyItemName"))
                    Return
                End If
                If Not Me.OtherDirtyItemName() Then '髒亂種類選擇[其他]而未填入值
                    MyObj.ShowMessage(Me.GetLocalResourceObject("MustOtherDirtyItemName"))
                Else
                    RaiseEvent Saving(Me, New EventArgs)
                End If

                '寄信通知公所及工務段人員
                sql.Length = 0
                sql.AppendFormat("select b.OrganizationName,b.Email from FD_OrganizationRegionMapping a ")
                sql.AppendFormat("join Organization b on a.OrganizationID=b.OrganizationID ")
                sql.AppendFormat("where a.RegionID='{0}' ", Me.TownID)
                If TryCast(Me.fv.FindControl("rboRegionType"), RadioButtonList).SelectedItem.Text = "道路" Then
                    sql.AppendFormat("union ")
                    sql.AppendFormat("select b.OrganizationName,b.Email from FD_OrganizationRoadMapping a ")
                    sql.AppendFormat("join Organization b on a.OrganizationID=b.OrganizationID ")
                    sql.AppendFormat("where a.RoadID='{0}' ", Me.RoadID)
                End If
                dt = dat.GetData(sql.ToString())
                'MyObj.WriteLog(sql.ToString())
                For i As Integer = 0 To dt.Rows.Count - 1
                    If MyObj.EmailAddressCheck(dt.Rows(i)("Email").ToString()) Then
                        Me.SendMail(dt.Rows(i)("OrganizationName").ToString(), dt.Rows(i)("Email").ToString())
                    End If
                Next
            End If
        End If
    End Sub

    Protected Sub rboRegionType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rboRegionType As RadioButtonList = TryCast(sender, RadioButtonList)
        If rboRegionType.SelectedItem.Text = Me.GetLocalResourceObject("IsRoad") Then
            'TryCast(fv.FindControl("ddlRoadID"), DropDownList).Visible = True
            TryCast(fv.FindControl("ddlRoadItem"), DropDownList).Visible = True
        Else
            'TryCast(fv.FindControl("ddlRoadID"), DropDownList).Visible = False
            TryCast(fv.FindControl("ddlRoadItem"), DropDownList).Visible = False
        End If
        If rboRegionType.SelectedItem.Text = Me.rboOther Then
            TryCast(fv.FindControl("txtOtherRegionType"), TextBox).Visible = True
        Else
            TryCast(fv.FindControl("txtOtherRegionType"), TextBox).Visible = False
        End If

        TryCast(fv.FindControl("txtOtherRegionType"), TextBox).Text = ""

        If Me.SelectedRegionType = Me.GetLocalResourceObject("IsRoad") And rboRegionType.SelectedItem.Text <> Me.GetLocalResourceObject("IsRoad") Then
            Me.BindRoadID()
            Me.BindRoadItem()
        ElseIf Me.SelectedRegionType <> Me.GetLocalResourceObject("IsRoad") And rboRegionType.SelectedItem.Text = Me.GetLocalResourceObject("IsRoad") Then
            Me.BindRoadID()
            Me.BindRoadItem()
        End If
        Me.SelectedRegionType = rboRegionType.SelectedItem.Text
    End Sub

End Class

Partial Class WebForm_SelfManageWorkHeader_SelfAddWorkHeader

#Region "初始化資料"
    '新增
    Public Sub InitData()
        Me.DataAccessMode = My.DataAccessMode.Insert
        Me.fv.ChangeMode(FormViewMode.Insert)
        Me.fv.DataBind()
    End Sub
#End Region

#Region "繫結資料"
    Private Sub BindDirtyItemForInsert()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select Value as DirtyItemID ")
        sql.AppendFormat(", [Text] as DirtyItemName ")
        sql.AppendFormat("from FD_CodeMapping ")
        sql.AppendFormat(" where TableName='FindDirtyItem' and ColumnName='DirtyItemName' order by Sequence")

        AddHandler TryCast(Me.fv.FindControl("gvDirtyItem"), GridView).RowDataBound, AddressOf Me.gv_RowDataBound
        TryCast(Me.fv.FindControl("gvDirtyItem"), GridView).DataSource = dat.GetData(sql)
        TryCast(Me.fv.FindControl("gvDirtyItem"), GridView).DataBind()

    End Sub

    Private Sub BindTownID()
        Dim ddl As DropDownList = TryCast(fv.FindControl("ddlTownID"), DropDownList)
        If ddl IsNot Nothing Then
            ddl.DataTextField = "RegionName"
            ddl.DataValueField = "RegionID"
            ddl.DataSource = MyObj.GetTownData("TPC")
            ddl.DataBind()

            ddl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
        End If
    End Sub

    Private Sub BindRoadID()
        Dim ddl As DropDownList = TryCast(fv.FindControl("ddlRoadID"), DropDownList)
        If ddl IsNot Nothing Then
            ddl.DataTextField = "RoadName"
            ddl.DataValueField = "RoadID"
            Dim rboRegionType As RadioButtonList = TryCast(fv.FindControl("rboRegionType"), RadioButtonList)
            If rboRegionType.SelectedIndex <> -1 AndAlso rboRegionType.SelectedItem.Text = Me.GetLocalResourceObject("IsRoad") Then
                ddl.DataSource = MyObj.GetRoadData(TryCast(fv.FindControl("ddlTownID"), DropDownList).SelectedValue)
                ddl.DataBind()
                If ddl.Items.Count > 0 Then
                    ddl.Visible = True
                Else
                    ddl.Visible = False
                End If
                ddl.Items.Insert(0, New ListItem("其他道路", "0"))
            Else
                ddl.DataSource = MyObj.GetRegionRoadData(TryCast(fv.FindControl("ddlTownID"), DropDownList).SelectedValue)
                ddl.DataBind()
                If ddl.Items.Count > 0 Then
                    ddl.Visible = True
                Else
                    ddl.Visible = False
                End If
            End If
        End If

    End Sub

    Private Sub BindRoadItem()
        Dim ddl As DropDownList = TryCast(fv.FindControl("ddlRoadItem"), DropDownList)
        If ddl IsNot Nothing Then
            ddl.DataTextField = "RoadItemName"
            ddl.DataValueField = "RoadItemID"
            ddl.DataSource = MyObj.GetRoadItemData(TryCast(fv.FindControl("ddlRoadID"), DropDownList).SelectedValue)
            ddl.DataBind()

            Dim rboRegionType As RadioButtonList = TryCast(fv.FindControl("rboRegionType"), RadioButtonList)
            If ddl.Items.Count > 0 And TryCast(fv.FindControl("ddlTownID"), DropDownList).Visible = True Then
                ddl.Visible = True
                If (rboRegionType.SelectedIndex = -1) OrElse (rboRegionType.SelectedItem.Text <> Me.GetLocalResourceObject("IsRoad")) Then
                    ddl.Visible = False
                End If
            Else
                ddl.Visible = False
            End If
        End If

    End Sub

    Private Sub BindDirtyLevel(ByVal ddl As DropDownList)
        If ddl IsNot Nothing Then
            ddl.DataTextField = "Text"
            ddl.DataValueField = "Value"
            ddl.DataSource = MyObj.GetCodeMappingData("FindDirtyItem", "DirtyLevel")
            ddl.DataBind()

            ddl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
        End If

    End Sub

    Private Sub BindRegionType()
        Dim rbo As RadioButtonList = TryCast(Me.fv.FindControl("rboRegionType"), RadioButtonList)
        If rbo IsNot Nothing Then
            rbo.DataTextField = "Text"
            rbo.DataValueField = "Value"
            rbo.DataSource = MyObj.GetCodeMappingData("FindDirtyHeader", "RegionType")
            rbo.DataBind()
        End If


    End Sub
#End Region

#Region "方法"
    '寄信通知公所及工務段
    Private Function SendMail(ByVal OrganizationName As String, ByVal Email As String)
        Try
            Dim objSMTP As New SmtpClient()
            Dim objMailMessage As New MailMessage()
            Dim SentContent As String = ""
            SentContent = OrganizationName & " 您好："
            SentContent &= "<br>" & "<br>" & "案件編號：" & Me.CasePassword
            SentContent &= "<br>" & "通報日期：" & Me.ReportDate.ToString("yyyy/MM/dd")
            SentContent &= "<br>" & "地點：" & Me.RoadItemName & Me.Location()
            SentContent &= "<br>" & "主旨：" & Me.Topic()
            SentContent &= "<br>" & "髒亂描述：" & Me.Description()
            SentContent &= "<br>" & "<br>" & "請盡速前往處理!!謝謝!!"
            SentContent &= "<br>" & "台北縣環保局  敬上"

            objMailMessage.From = New MailAddress(MyObj.GetParameter("Home", "MailSender"))
            objMailMessage.To.Add(New MailAddress(Email))
            objMailMessage.Subject = "台北縣環保局環境污染通報系統通知"
            objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8
            objMailMessage.Body = SentContent
            objMailMessage.BodyEncoding = System.Text.Encoding.UTF8
            objMailMessage.IsBodyHtml = True
            objSMTP.Send(objMailMessage)
            Return True
        Catch ex As Exception
            MyObj.WriteLog(ex.ToString())
            Return False
        End Try
    End Function

    '上傳頁面
    Private Function UploadString(ByVal strID, ByVal strType) As String
        Dim ReturnString As String = ""
        ReturnString = "window.showModalDialog('" & Me.ResolveUrl("~/Public/UploadFile.aspx") & "?CasePassword=" & strID & "&UploadType=" & strType & "&thetime=" & DateTime.Now.ToString("hhmmss") & "', 'UploadFile', 'dialogHeight:500px;dialogWidth:800px;status:no,resizable:no,scrollbars:auto,toolbar:no,location:no,menubar:no;'); return false;"
        Return ReturnString
    End Function
#End Region

#Region "屬性"

    Public Property DataAccessMode() As My.DataAccessMode
        Get
            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As My.DataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property

    Public Property FindDirtyHeaderID() As String
        Get
            Return ViewState("FindDirtyHeaderID")
        End Get
        Set(ByVal value As String)
            ViewState("FindDirtyHeaderID") = value
        End Set
    End Property

    Public Property SelectedRegionType() As String
        Get
            Return ViewState("SelectedRegionType")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedRegionType") = value
        End Set
    End Property

    Public ReadOnly Property OtherDirtyItemName() As Boolean
        Get
            Dim gvDirtyItem As GridView = TryCast(Me.fv.FindControl("gvDirtyItem"), GridView)
            Dim chkDirtyItemName As CheckBox
            Dim txtDirtyItemName As TextBox

            For i As Integer = 0 To gvDirtyItem.Rows.Count - 1
                chkDirtyItemName = TryCast(gvDirtyItem.Rows(i).FindControl("chkDirtyItemName"), CheckBox)
                txtDirtyItemName = TryCast(gvDirtyItem.Rows(i).FindControl("txtDirtyItemName"), TextBox)
                If chkDirtyItemName.Checked And chkDirtyItemName.Text = Me.gvDirtyItem Then
                    If txtDirtyItemName.Text.Trim() = "" Then
                        Return False
                    End If
                End If
            Next
            Return True
        End Get
    End Property

    '髒亂地區類別
    Public ReadOnly Property RegionType() As String
        Get
            If TryCast(Me.fv.FindControl("rboRegionType"), RadioButtonList).SelectedItem.Text = Me.rboOther Then
                Return TryCast(Me.fv.FindControl("txtOtherRegionType"), TextBox).Text.Trim()
            Else
                Return TryCast(Me.fv.FindControl("rboRegionType"), RadioButtonList).SelectedItem.Text
            End If
        End Get
    End Property

    '案件代碼
    Public ReadOnly Property RandomCasePassword() As String
        Get
            Return TryCast(Me.fv.FindControl("txtRandomCasePassword"), TextBox).Text
        End Get
    End Property

    '案件編號
    Public ReadOnly Property CasePassword() As String
        Get
            Return TryCast(Me.fv.FindControl("txtCasePassword"), TextBox).Text
        End Get
    End Property

    'CreateBy
    Public ReadOnly Property CreateBy() As String
        Get
            Return TryCast(Me.fv.FindControl("txtCreateBy"), TextBox).Text
        End Get
    End Property

    'CreateTEL
    Public ReadOnly Property CreateTEL() As String
        Get
            Return TryCast(Me.fv.FindControl("txtCreateTEL"), TextBox).Text
        End Get
    End Property

    'CreateEmail
    Public ReadOnly Property CreateEmail() As String
        Get
            Return TryCast(Me.fv.FindControl("txtCreateEmail"), TextBox).Text
        End Get
    End Property

    'TownID
    Public ReadOnly Property TownID() As String
        Get
            Return TryCast(Me.fv.FindControl("ddlTownID"), DropDownList).SelectedValue
        End Get
    End Property

    'RoadID
    Public ReadOnly Property RoadID() As String
        Get
            Dim ddlRoadID As DropDownList
            ddlRoadID = TryCast(Me.fv.FindControl("ddlRoadID"), DropDownList)
            Return ddlRoadID.SelectedValue
            'If ddlRoadID.Visible Then
            '    Return ddlRoadID.SelectedValue
            'Else
            '    Return TryCast(Me.fv.FindControl("ddlTownID"), DropDownList).SelectedValue
            'End If
        End Get
    End Property

    'RoadItemID
    Public ReadOnly Property RoadItemID() As String
        Get
            Dim ddlRoadItem As DropDownList
            ddlRoadItem = TryCast(Me.fv.FindControl("ddlRoadItem"), DropDownList)
            If ddlRoadItem.Visible Then
                Return ddlRoadItem.SelectedValue
            Else
                Return ""
            End If
        End Get
    End Property

    'RoadItemName
    Public ReadOnly Property RoadItemName() As String
        Get
            Dim ddlRoadItem As DropDownList
            ddlRoadItem = TryCast(Me.fv.FindControl("ddlRoadItem"), DropDownList)
            Dim ReturnValue As String = ""
            If ddlRoadItem.Visible Then
                Try
                    ReturnValue = ddlRoadItem.SelectedItem.Text
                Catch ex As Exception
                    ReturnValue = ""
                End Try
            Else
                ReturnValue = ""
            End If
            Return ReturnValue
        End Get
    End Property

    'Location
    Public ReadOnly Property Location() As String
        Get
            Return TryCast(Me.fv.FindControl("txtLocation"), TextBox).Text.Trim()
        End Get
    End Property

    'ReportDate
    Public ReadOnly Property ReportDate() As DateTime
        Get
            Return DateTime.Parse(TryCast(Me.fv.FindControl("txtReportDate"), TextBox).Text)
        End Get
    End Property

    'Topic
    Public ReadOnly Property Topic() As String
        Get
            Return TryCast(Me.fv.FindControl("txtTopic"), TextBox).Text.Trim()
        End Get
    End Property

    'Description
    Public ReadOnly Property Description() As String
        Get
            Return TryCast(Me.fv.FindControl("txtDescription"), TextBox).Text.Trim()
        End Get
    End Property

    Public HaveItem As Boolean
    Private dirtyItemField As String(,)

    '髒亂種類/程度(DirtyItemName、DirtyLevel)
    Public Property DirtyItem() As String(,)
        Get
            Return dirtyItemField
        End Get
        Set(ByVal value As String(,))
            dirtyItemField = value
        End Set
    End Property

    '取髒亂種類/程度(DirtyItemName、DirtyLevel)
    Public Function SetDirtyItem() As String(,)
        Me.HaveItem = False
        Dim gvDirtyItem As GridView = TryCast(Me.fv.FindControl("gvDirtyItem"), GridView)
        Dim GetItem(gvDirtyItem.Rows.Count - 1, 1) As String
        Dim chkDirtyItemName As CheckBox
        Dim txtDirtyItemName As TextBox
        'Dim ddlDirtyLevel As DropDownList
        Dim txtDirtyLevel As TextBox

        For i As Integer = 0 To gvDirtyItem.Rows.Count - 1
            chkDirtyItemName = TryCast(gvDirtyItem.Rows(i).FindControl("chkDirtyItemName"), CheckBox)
            'ddlDirtyLevel = TryCast(gvDirtyItem.Rows(i).FindControl("ddlDirtyLevel"), DropDownList)
            txtDirtyLevel = TryCast(gvDirtyItem.Rows(i).FindControl("txtDirtyLevel"), TextBox)
            ' If ddlDirtyLevel.SelectedValue <> Me.GetLocalResourceObject("MustSelect") Then
            If chkDirtyItemName.Checked Then
                Me.HaveItem = True
                txtDirtyItemName = TryCast(gvDirtyItem.Rows(i).FindControl("txtDirtyItemName"), TextBox)
                If chkDirtyItemName.Text = Me.gvDirtyItem Then
                    If txtDirtyItemName.Text.Trim() <> "" Then
                        GetItem(i, 0) = txtDirtyItemName.Text.Trim()
                        'GetItem(i, 1) = ddlDirtyLevel.SelectedValue
                        GetItem(i, 1) = txtDirtyLevel.Text.Trim
                    End If
                Else
                    GetItem(i, 0) = chkDirtyItemName.Text
                    'GetItem(i, 1) = ddlDirtyLevel.SelectedValue
                    GetItem(i, 1) = txtDirtyLevel.Text.Trim
                End If
            End If
        Next
        Return GetItem
    End Function
#End Region

#Region "Raise事件"
    Public Event Saving(ByVal sender As Object, ByVal e As EventArgs)
#End Region
End Class